package com.lq.demo.demo.mode.websocket;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.*;
import java.io.IOException;

/**
 * WebSocket 处理器
 */
@Component
public class MessageWebSocketHandler implements WebSocketHandler {
    private WebSocketSession session;

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws IOException {
        this.session = session;
        System.out.println(" create connect  success");

    }

    public void send() throws IOException {
        TextMessage message = new TextMessage("i'm  a  msg  from  server");
        session.sendMessage(message);
    }

    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) {
        System.out.println("  accept  msg  from  client");
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) {
        System.out.println("    connected  error ");

    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) {
        System.out.println("   after connectiong  closed ");
    }

    /**
     * Whether to process fragmented messages
     */
    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
}